﻿#region Legal notice
/*
 * This file is part of EPPLib.
 * Copyright 2010, 2011 
 * Gianluca Esposito (info@gianlucaesposito.it)
 * ----------------------------------------------------------------------------
 * EPPLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * EPPLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Nome-Programma; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EPPLib.Data;
using EPPLib.Managers;
using EPPLib.Messages;

namespace EPPWebMVC.Controllers
{
    public class DomainController : BaseController
    {
        [Authorize]
        public ActionResult Index()
        {
            DomainDataCollection contacts = Data.ReadDomains(CurrentUserID);
            SetBaseViewData();
            return View(contacts);
        }

        [Authorize]
        public ActionResult Details(string name)
        {
            SetBaseViewData();
            return View();
        }

        [Authorize]
        public ActionResult Create()
        {
            CountryDataCollection countries = Data.ReadCountries();
            EntityTypeDataCollection types = Data.ReadEntityTypes();

            //ViewData["__COUNTRIES"] = new SelectList(countries, "Code", "Description", DEFAULT_COUNTRY);
            //ViewData["__PHONE_PREFIXES"] = new SelectList(countries, "PhonePrefix", "PhonePrefix", DEFAULT_PREFIX);
            //ViewData["__FAX_PREFIXES"] = new SelectList(countries, "PhonePrefix", "PhonePrefix", DEFAULT_PREFIX);
            //ViewData["__ENTITY_TYPES"] = new SelectList(types, "Value", "Description");

            SetBaseViewData();
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(DomainDataObject data)
        {
            data.OwnerID = CurrentUserID;

            //CountryDataCollection countries = Data.ReadCountries();
            //EntityTypeDataCollection types = Data.ReadEntityTypes();
            //ViewData["__COUNTRIES"] = new SelectList(countries, "Code", "Description", data.CountryCode);
            //ViewData["__PHONE_PREFIXES"] = new SelectList(countries, "PhonePrefix", "PhonePrefix", data.PhonePrefix);
            //ViewData["__FAX_PREFIXES"] = new SelectList(countries, "PhonePrefix", "PhonePrefix", data.FaxPrefix);
            //ViewData["__ENTITY_TYPES"] = new SelectList(types, "Value", "Description", data.EntityType);

            if (ModelState.IsValid)
            {
                if (!Service.IsAuthenticated)
                {
                    LoginMessage loginRequest = new LoginMessage();
                    loginRequest.RegistrarID = CurrentAccount;
                    loginRequest.Password = CurrentPassword;
                    LoginResponseMessage loginResponse = Service.Login(CurrentTargetUrl, loginRequest);
                    if (loginResponse.Response.Status == EPPLib.Objects.ResponseObject.ResponseStatus.Done)
                    {
                        Service.IsAuthenticated = true;
                    }
                    else
                    {
                        Service.IsAuthenticated = false;
                    }
                }

                CreateDomainMessage message = new CreateDomainMessage();
                // message.Registrant = data.CurrentRegistrar;
                message.Name = data.Name;
                //CreateContactMessage contactMessage = new CreateContactMessage();
                //contactMessage.ContactID = data.ID;
                //contactMessage.PostalInfo.Name = data.Name;
                //contactMessage.PostalInfo.Address.Address[0] = data.Street1;
                //contactMessage.PostalInfo.Address.Address[1] = data.Street2;
                //contactMessage.PostalInfo.Address.Address[2] = data.Street3;
                //contactMessage.PostalInfo.Address.City = data.City;
                //contactMessage.PostalInfo.Address.District = data.District;
                //contactMessage.PostalInfo.Address.PostalCode = data.PostalCode;
                //contactMessage.PostalInfo.Address.CountryCode = data.CountryCode;
                //if (!string.IsNullOrEmpty(data.PhonePrefix) && !string.IsNullOrEmpty(data.Phone))
                //{
                //    contactMessage.Phone.Number = string.Format("{0}.{1}", data.PhonePrefix, data.Phone);
                //    contactMessage.Phone.Internal = data.PhoneInternal;
                //}
                //if (!string.IsNullOrEmpty(data.FaxPrefix) && !string.IsNullOrEmpty(data.Fax))
                //{
                //    contactMessage.Fax.Number = string.Format("{0}.{1}", data.FaxPrefix, data.Fax);
                //    contactMessage.Fax.Internal = data.Fax;
                //}
                //contactMessage.Email = data.Email;

                //CreateExtendedContactMessage extendedContactMessage = new CreateExtendedContactMessage();
                //extendedContactMessage.ConsentForPublishing = data.ConsentForPublishing;
                //extendedContactMessage.Registrant.EntityType = data.EntityType;
                //extendedContactMessage.Registrant.NationalityCode = data.Name;
                //extendedContactMessage.Registrant.TaxCode = data.RegCode;

                //CreateContactResponseMessage responseMessage = Service.CreateContact(CurrentTargetUrl, CurrentAccount, CurrentPassword,
                //    contactMessage,
                //    extendedContactMessage,
                //    MessageContainer.ComposeClientTransactionID(CurrentAccount, Data.IncreaseOperationCount(CurrentUserID)));

                //if (responseMessage.Response.Status == EPPLib.Objects.ResponseObject.ResponseStatus.Done)
                //{
                    Data.SaveDomain(data);
                //}

                //SetBaseViewData(responseMessage);
            }
            else
            {
                SetBaseViewData();
            }
            return View();
        }

        //
        // GET: /Domain/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            SetBaseViewData();
            return View();
        }

        //
        // POST: /Domain/Edit/5

        [HttpPost]
        [Authorize]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                SetBaseViewData();
                return View();
            }
        }

        [Authorize]
        public ActionResult Delete(string name)
        {
            ViewData["__DOMAIN_NAME"] = name;
            SetBaseViewData();
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult Delete(FormCollection data)
        {
            DeleteDomainMessage message = new DeleteDomainMessage();
            if (!string.IsNullOrEmpty(data["nameBox"])) message.Name = data["nameBox"];
            DeleteDomainResponseMessage responseMessage = Service.DeleteDomain(CurrentTargetUrl, CurrentAccount, CurrentPassword, message, MessageContainer.ComposeClientTransactionID(CurrentAccount, Data.IncreaseOperationCount(CurrentUserID)));
            if (responseMessage.Response.Status == EPPLib.Objects.ResponseObject.ResponseStatus.Done)
                Data.DeleteDomain(data["nameBox"], CurrentUserID);

            SetBaseViewData(responseMessage);
            return View();
        }

        [Authorize]
        public ActionResult Check()
        {
            SetBaseViewData();
            return View();
        }
        [Authorize]
        [HttpPost]
        public ActionResult Check(FormCollection data)
        {
            CheckDomainMessage message = new CheckDomainMessage();
            if (!string.IsNullOrEmpty(data["nameBox1"])) message.Names.Add(data["nameBox1"]);
            if (!string.IsNullOrEmpty(data["nameBox2"])) message.Names.Add(data["nameBox2"]);
            if (!string.IsNullOrEmpty(data["nameBox3"])) message.Names.Add(data["nameBox3"]);
            if (!string.IsNullOrEmpty(data["nameBox4"])) message.Names.Add(data["nameBox4"]);
            if (!string.IsNullOrEmpty(data["nameBox5"])) message.Names.Add(data["nameBox5"]);

            CheckDomainResponseMessage response = Service.CheckDomain(CurrentTargetUrl, CurrentAccount, CurrentPassword, message, MessageContainer.ComposeClientTransactionID(CurrentAccount, Data.IncreaseOperationCount(CurrentUserID)));
            SetBaseViewData(response);
            TempData["__ISDONE"] = (response.Response.Status == EPPLib.Objects.ResponseObject.ResponseStatus.Done);
            TempData["__REQUEST"] = response.RawRequestData;
            TempData["__RESPONSE"] = response.RawResponseData;
            TempData["__SESSION"] = Service.Session;
            return View();
        }

        [Authorize]
        public ActionResult Info()
        {
            SetBaseViewData();
            return View();
        }
        [Authorize]
        [HttpPost]
        public ActionResult Info(FormCollection data)
        {
            InfoDomainMessage message = new InfoDomainMessage();
            if (!string.IsNullOrEmpty(data["nameBox"])) message.DomainName.Name = data["nameBox"];

            InfoDomainResponseMessage responseMessage = Service.InfoDomain(CurrentTargetUrl, CurrentAccount, CurrentPassword, message, MessageContainer.ComposeClientTransactionID(CurrentAccount, Data.IncreaseOperationCount(CurrentUserID)));
            SetBaseViewData(responseMessage);

            return View();
        }


        [Authorize]
        public ActionResult Transfer()
        {
            SetBaseViewData();
            return View();
        }

        [Authorize]
        public ActionResult TransferTrade()
        {
            SetBaseViewData();
            return View();
        }

        [Authorize]
        public ActionResult TransferApprove()
        {
            SetBaseViewData();
            return View();
        }

        [Authorize]
        public ActionResult TransferReject()
        {
            SetBaseViewData();
            return View();
        }

        [Authorize]
        public ActionResult TransferCancel()
        {
            SetBaseViewData();
            return View();
        }
    }
}
